﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Toolbox
{
    class Apriori
    {
            public class Item : IComparable       
            {
                public string Name { get; set; }
                public double Support { get; set; }

                public Item(string s)
                {
                    Name = s;
                }
                
                public int CompareTo(object obj)
                {
                    return Name.CompareTo(((Item)obj).Name);
                }

                public override string ToString()
                {
                    return Name;
                }
            }

            public class ItemSet
            {
                public ArrayList Items = new ArrayList();
                public void Add(Item it)
                {
                    Items.Add(it);
                    Items.Sort();
                }

                public override string ToString()
                {
                    string res = "";
                    foreach (Item i in Items)
                        res += i.ToString() + " ";
                    res = res.Remove(res.Length-1);
                    return res;
                }

                public override bool Equals(object obj)
                {
                    ItemSet its = (ItemSet)obj;
                    if (its.Items.Count != Items.Count) return false;
                    else
                    {
                        
                    }
                }
            }
            public class ItemMatrix
            {
                public List<Apriori.ItemSet> Matrix = new List<Apriori.ItemSet>();
                public ItemMatrix() { }
                public ItemMatrix(string s)
                {
                    char[] sep1 = new char[] { '\n' };
                    char[] sep2 = new char[] { ' ' };                    
                    s = s.Replace("\r\n", "\n");
                    string[] lines = s.Split(sep1);
                    for (int i = 0; i < lines.Length; i++)
                    {
                        if (lines[i] == "") continue;
                        Apriori.ItemSet set = new Apriori.ItemSet();
                        string[] items = lines[i].Split(sep2);
                        for (int j = 0; j < items.Length; j++)
                        {
                            Apriori.Item tmp = new Apriori.Item(items[j]);
                            set.Add(tmp);
                        }
                        Matrix.Add(set);
                    }
                }
            }

            public static ItemMatrix GetCandidate(ItemMatrix d, int minsupp)
            {
                ItemMatrix res = new ItemMatrix();

                foreach (ItemSet IS in d.Matrix)
                {
                    foreach (Item i in IS.Items)
                    {
                        ItemSet tmp = new ItemSet();
                        tmp.Items.Add(i);
                    }
                }

                return res;
            
            }
    }
}
